#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.dirname(File.realpath($PROGRAM_NAME))))

require "#{LKP_SRC}/lib/statistics"

time = []

while (line = gets)
  case line
  when /^Running in .* mode with (\d+) groups using (\d+) file descriptors each \(== (\d+) tasks\)/
    tasks = $3.to_i
  when /^Each sender will pass (\d+) messages of (\d+) bytes/
    messages = $1.to_i
    bytes = $2.to_i
  when /^Each sender passed (\d+) messages of (\d+) bytes in average/
    messages = $1.to_i
    bytes = $2.to_i
  when /^Time:/
    _name, seconds = line.split
    time << seconds.to_f
  end
end

kb = tasks * messages * bytes >> 10
best_time = time.min
worst_time = time.max
avg_time = time.sum(0.0) / time.size
# Historically, best time is used to deliver throughput, but
# it's not obvious that best time is used so explicitely output
# throughput for all 3 times. Keep throughput as its original
# meanning to avoid triggering bisect.
puts "throughput: #{kb / best_time}"
puts "throughput_best: #{kb / best_time}"
puts "throughput_avg: #{kb / avg_time}"
puts "throughput_worst: #{kb / worst_time}"
